<template>
	<view class="">
		<view class="u-p-t-30">
			<u-cell-group>
				<u-cell-item title="个人资料" :title-style="{fontSize:'30rpx'}" index="/pages/subpage/profile/profile"
					@click="cellClick"></u-cell-item>
				<u-cell-item title="账号安全" :title-style="{fontSize:'30rpx'}" index="/pages/subpage/setting/safe"
					@click="cellClick"></u-cell-item>


				<!-- #ifdef APP-PLUS -->
				<u-cell-item title="消息推送" :title-style="{fontSize:'30rpx'}" index="5" @click="cellClick" :arrow="false">
					<u-switch slot="right-icon" v-model="isPush" :active-color="$appConfig.mainColor" size="40"
						@change="pushChange" inactive-color="#aaa" :loading="loading"></u-switch>
				</u-cell-item>
				<u-cell-item title="当前版本" :value="version" :title-style="{fontSize:'30rpx'}" index="4"
					@click="cellClick"></u-cell-item>
				<!-- #endif -->
				<u-cell-item title="注销账号" :title-style="{fontSize:'30rpx'}" index="3" @click="cellClick"></u-cell-item>
			</u-cell-group>
		</view>
		<view class="u-p-t-30">
			<view class="bg-white u-p-32 u-type-primary align-center u-font-32 " @click="logOut">
				退出登录
			</view>
		</view>

		<u-modal v-model="showmodel" :show-title="false" :show-cancel-button="true" :confirm-color="$appConfig.mainColor"
			@confirm="cancelAcount">
			<view class="slot-content">
				<image src="/static/signOut.png" mode="widthFix" style="width: 100%;"></image>
				<view class="model-content">
					<view class="u-m-b-30 u-font-36">确定要注销账号吗？</view>
					<view class="u-font-28 u-tips-color">注销账号将永久失效且不可恢复，并将放弃权益资产与服务</view>
				</view>
			</view>
		</u-modal>

		<u-modal v-model="loading" :show-cancel-button="true" :content="isPush?'确定关闭消息推送？关闭后不能及时收到平台通知消息':'是否开启平台消息推送'"
			:confirm-color="$appConfig.mainColor" @confirm="getRestultFromServer"></u-modal>
	</view>
</template>

<script>
	import common from '@/utils/wxApplet/common.js'
	import APPUpdate, {
		getCurrentNo
	} from '@/uni_modules/zhouWei-APPUpdate/js_sdk/appUpdate';
	export default {
		data() {
			return {
				showmodel: false,
				version: '',
				isPush: true,
				controlStatus: false,
				loading: false
			}
		},
		onLoad() {
			const userInfo = this.$store.state.vuex_userInfo
			this.isPush = userInfo.notice_set.all == 1
			// #ifdef APP-PLUS
			getCurrentNo(res => {
				// 进页面获取当前APP版本号（用于页面显示）
				console.log(res);
				this.version = res.versionName;
			});
			// #endif
		},
		methods: {
			cellClick(index) {
				if (index == 3) {
					this.showmodel = true
				} else if (index == 4) {
					APPUpdate(true);
				} else if (index.indexOf('page') >= 0) {
					this.$u.route(index)
				}
			},
			pushChange(status) {
				if (this.controlStatus == true) {
					this.controlStatus = false;
					return;
				}
				this.isPush = !status;
				this.loading = true;
			},
			getRestultFromServer() {
				const open = this.isPush ? 0 : 1
				this.$u.api.pushSet({
					open: open,
					type: 'all'
				}).then(res => {
					this.isPush = res.all == 1
				})
			},
			cancelAcount() {
				// 注销
				this.$u.api.cancelUser().then(res => {
					this.$u.toast('帐号已注销')
					common.logout()
					this.$u.vuex('vuex_openid', '');
					common.wxLogin()
					uni.switchTab({
						url: '/pages/center/index'
					})
				})
			},
			logOut() {
				// 退出登录
				uni.showModal({
					content: '确定要退出登录吗？',
					success: async (res) => {
						if (res.confirm) {
							// 退出
							await this.$u.api.wxLogOut()
							common.logout()
							uni.switchTab({
								url: '/pages/index/index'
							})
						}
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.model-content {
		padding: 30rpx 30rpx 35rpx 40rpx;
	}
</style>